import { getColorTextLoadMap, tooltipLength } from '@/utils';
import { Button, Tooltip } from 'antd';
import { DesignModelAPI } from '../common/type';
import pageStyles from '@/common/pageLayout.less';
import { Fragment } from 'react';
import { EditOutlined, SisternodeOutlined, SendOutlined } from '@ant-design/icons';

/** 表格功能操作 */
const getManageFunctions = (
  /** 资源权限参数 */
  curRes: Record<string, any>,
  /** 数据操作参数 */
  operateFnc: DesignModelAPI.IOperationClick,
  /** 数据操作对象 */
  record: Record<string, any>,
) => {
  /** 功能操作集 */
  let operationList: any = [];

  /**
   * 编辑
   */
  const Edit = !!curRes['/activitiController/modelEdit'] && record?.isRelease == '0' && (
    <Fragment key={record?.id + 'EditOutlined'}>
      <Button
        className="manageBtntTxtStyle"
        onClick={() => {
          operateFnc?.onEdit(record);
        }}
        type={'link'}
        size={'small'}
        icon={<EditOutlined />}
      >
        编辑
      </Button>
    </Fragment>
  );

  /**
   * 设计
   */
  const Process = !!curRes['/activitiController/modelDesign/{modelId}'] && (
    // record?.isRelease == '0' &&
    <Fragment key={record?.id + 'SisternodeOutlined'}>
      <Button
        className="manageBtntTxtStyle"
        onClick={() => {
          operateFnc?.onProcess(record);
        }}
        type={'link'}
        size={'small'}
        icon={<SisternodeOutlined />}
      >
        设计
      </Button>
    </Fragment>
  );

  /**
   * 发布
   */
  const Release = !!curRes['/activitiController/modelDeploy'] && (
    <Fragment key={record?.id + 'SendOutlined'}>
      <Button
        className="manageBtntTxtStyle"
        onClick={() => {
          operateFnc?.onRelease(record);
        }}
        type={'link'}
        size={'small'}
        icon={<SendOutlined />}
      >
        发布
      </Button>
    </Fragment>
  );

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  operationList = [Edit, Process, Release];
  operationList = operationList?.filter((itemBtn: any) => itemBtn);
  return operationList && operationList?.length > 0 ? operationList : [Disable];
};

/**
 * 表格信息 展示和操作
 * @returns
 */
const getDesignTableColumns = (
  /** 资源权限参数 */
  curRes: Record<string, any>,
  /** 数据操作参数 */
  operateFnc: DesignModelAPI.IOperationClick,
) => {
  return [
    {
      title: '所属单位',
      dataIndex: 'ownCompanyName',
      key: 'ownCompanyName',
      width: 150,
      fixed: 'left',
      visible: true,
      sorter: (a: any, b: any) => (a.ownCompanyID > b.ownCompanyID ? 1 : -1),
      render: (text: string) => {
        let { colorText, color } = getColorTextLoadMap(text);
        return (
          <Tooltip title={colorText}>
            <span style={{ color: color }} className={pageStyles['over-flow-hidden']}>
              {colorText ? colorText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '流程名称',
      dataIndex: 'name',
      key: 'name',
      width: 220,
      visible: true,
      unDrag: true,
      sorter: (a: any, b: any) => (a.name > b.name ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '工单类别',
      dataIndex: 'orderTypeName',
      key: 'orderTypeName',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => (a.orderTypeName > b.orderTypeName ? 1 : -1),
      render: (text: string) => {
        let { colorText, color } = getColorTextLoadMap(text);
        return (
          <Tooltip title={colorText}>
            <span style={{ color: color }} className={pageStyles['over-flow-hidden']}>
              {colorText ? colorText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '流程类别',
      dataIndex: 'categoryName',
      key: 'categoryName',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a.categoryName > b.categoryName ? 1 : -1),
      render: (text: string) => {
        let { colorText, color } = getColorTextLoadMap(text);
        return (
          <Tooltip title={colorText}>
            <span style={{ color: color }} className={pageStyles['over-flow-hidden']}>
              {colorText ? colorText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '业务类别',
      dataIndex: 'businessCategoryName',
      key: 'businessCategoryName',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a.businessCategoryName > b.businessCategoryName ? 1 : -1),
      render: (text: string) => {
        let { colorText, color } = getColorTextLoadMap(text);
        return (
          <Tooltip title={colorText}>
            <span style={{ color: color }} className={pageStyles['over-flow-hidden']}>
              {colorText ? colorText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => (a.createTime > b.createTime ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span className={pageStyles['over-flow-hidden']} style={{ maxWidth: 140 }}>
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '是否发布',
      dataIndex: 'isRelease',
      key: 'isRelease',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => a.isRelease - b.isRelease,
      render: (text: string) => {
        let newText = '';
        let textColor = '';
        if (text == '0') {
          newText = '未发布';
          textColor = '#ff3700';
        } else if (text == '1') {
          newText = '已发布';
          textColor = '#42c900';
        }
        return <span style={{ color: textColor }}>{newText ? newText : '--'}</span>;
      },
    },
    {
      title: '流程标识',
      dataIndex: 'keyWord',
      key: 'keyWord',
      width: 200,
      visible: false,
      sorter: (a: any, b: any) => (a.keyWord > b.keyWord ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']} style={{ maxWidth: 190 }}>
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      width: 150,
      visible: false,
      sorter: (a: any, b: any) => (a.modifyTime > b.modifyTime ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span className={pageStyles['over-flow-hidden']} style={{ maxWidth: 150 }}>
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '版本',
      dataIndex: 'version',
      key: 'version',
      width: 80,
      visible: false,
      sorter: (a: any, b: any) => (a.version > b.version ? 1 : -1),
      render: (text: string | null) => {
        return <span style={{ maxWidth: 70 }}>{text ? text : '--'}</span>;
      },
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 230,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (text: any, record: any) => {
        return (
          <div
            style={{
              display: 'flex',
            }}
          >
            {getManageFunctions(curRes, operateFnc, record)}
          </div>
        );
      },
    },
  ];
};

export { getDesignTableColumns };
